/**
**************************************************************************************
*Palisma - Secrets of the Illuminati is an open-source 2D RPG                        *
*Copyright (C) 2006, Tony Sparks                                                     *
*                                                                                    *
*This library is free software; you can redistribute it and/or                       *
*modify it under the terms of the GNU Lesser General Public                          *
*License as published by the Free Software Foundation; either                        *
*version 2.1 of the License, or (at your option) any later version.                  *
*                                                                                    *
*This library is distributed in the hope that it will be useful,                     *
*but WITHOUT ANY WARRANTY; without even the implied warranty of                      *
*MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU                   *
*Lesser General Public License for more details.                                     *
*                                                                                    *
*You should have received a copy of the GNU Lesser General Public                    *
*License along with this library; if not, write to the Free Software                 *
*Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA      *
**************************************************************************************
*/
#include "StdAfx.h"
#include "PlayerConfig.h"
#include "../kernel.h"
#include "../input/keyconverter.h"
#include "../shared/StringUtil.h"
#include "sdl.h"
#include <fstream>
#include <sstream>


extern Kernel* g_kernel;


PlayerConfig::PlayerConfig(void)
{
   m_registered = false;
   save_f.Set( this );

   g_kernel->GetConsole()->AddCommand( &save_f, "save_cfg");
}

/**
==================
Register player key binds
==================
*/
void PlayerConfig::Register()
{
    if ( !m_registered) {
        CMMPointer<Cvars> cvars = g_kernel->GetCvars();

        KeyConverter key;
        cvars->RegisterCvar( "+up", key.ToString( SDLK_UP ), SDLK_UP, CVAR_SAVE|CVAR_BIND );
        cvars->RegisterCvar( "+down", key.ToString( SDLK_DOWN ), SDLK_DOWN, CVAR_SAVE|CVAR_BIND );
        cvars->RegisterCvar( "+left", key.ToString( SDLK_LEFT ), SDLK_LEFT, CVAR_SAVE|CVAR_BIND );
        cvars->RegisterCvar( "+right", key.ToString( SDLK_RIGHT ), SDLK_RIGHT, CVAR_SAVE|CVAR_BIND );
        cvars->RegisterCvar( "+attack1", key.ToString( SDLK_SPACE ), SDLK_SPACE, CVAR_SAVE|CVAR_BIND );
        cvars->RegisterCvar( "+use", key.ToString( SDLK_e ), SDLK_e, CVAR_SAVE|CVAR_BIND );
        cvars->RegisterCvar( "+strafe", key.ToString( SDLK_LSHIFT ), SDLK_LSHIFT, CVAR_SAVE|CVAR_BIND );
        
        // joystick
        cvars->RegisterCvar( "+j_up", key.ToString( 11 ), 10, CVAR_SAVE|CVAR_BIND );
        cvars->RegisterCvar( "+j_down", key.ToString( 2 ), 2, CVAR_SAVE|CVAR_BIND );
        cvars->RegisterCvar( "+j_left", key.ToString( 3 ), 3, CVAR_SAVE|CVAR_BIND );
        cvars->RegisterCvar( "+j_right", key.ToString( 12 ), 12, CVAR_SAVE|CVAR_BIND );
        cvars->RegisterCvar( "+j_attack1", key.ToString( 0 ), 0, CVAR_SAVE|CVAR_BIND );
        cvars->RegisterCvar( "+j_use", key.ToString( 2 ), 2, CVAR_SAVE|CVAR_BIND );//1,2,3,4 1=b,2=x, 3=y, 4= ltb, 5=rtb, 6=back, 7=start, 8=ljb, 9=rjb, 0=a
        cvars->RegisterCvar( "+j_strafe", key.ToString( 1 ), 1, CVAR_SAVE|CVAR_BIND );
        m_registered = true;
    }
}

/**
======================
Save the users configuration
======================
*/
int PlayerConfig::Save(const std::string &file)
{
    KeyConverter    key;
    std::ofstream   stream;

    stream.open( file.c_str() );

    stream << "%% --- " + file + " --- %%\n";
    stream << "%% Auto Generated by Palisma, please DO NOT modify! %%\n\n";
    
    std::map< std::string, cvar>::iterator it;
    int count = 0;
    /*
     * Also print anything that may have been binded
     */
    
    std::map<int, std::string >::iterator iter = g_kernel->GetInput()->GetBinder()->m_bindings.begin();
    for(; iter != g_kernel->GetInput()->GetBinder()->m_bindings.end();iter++ )
    {
        stream << "bind " +  key.ToString( iter->first) + " " + iter->second + ";\n";
    }
    /**
     * Print each cvar
     */
    for( it = g_kernel->GetCvars()->GetCvars().begin(); it != g_kernel->GetCvars()->GetCvars().end(); )
    {
        ++count;
        if ( !(it->second.flags & CVAR_BIND) ) 
          //  stream << "bind " + it->second->svalue + " " + it->second->name + ";\n";
        //else
            stream << "set " + it->second.name + " " + it->second.svalue + ";\n";
        ++it;
    }



    g_kernel->LPrintf( ("Saving " + file + "...").c_str() );
    stream.close();
    return 0;
}

PlayerConfig::~PlayerConfig(void)
{
    g_kernel->GetConsole()->RemoveCommand( "save_cfg" );
}


/**
==================================
Save Command
==================================
*/
void SaveCFG_f::Exec(std::string &s)
{ 
    if ( m_config )
    {
        if ( s != "" )
            m_config->Save( s );
        else
            m_config->Save();
    }
};